package com.picvik.action;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.opensymphony.xwork2.ActionSupport;
import com.picvik.util.MyLog;
import com.picvik.util.RuntimeSettings;

public class EmailAction extends ActionSupport {
	
	   private String from;
	   private String password;
	   private String to;
	   private String subject;
	   private String body;
	   
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	
	// Create properties object
	static Properties properties = new Properties();
	   static
	   {
		  //Set mail SMTP host in properties
		  //proxy settings
		  //properties.put("proxySet","true");
		  //properties.put("socksProxyHost","192.16.3.254");
	      properties.put("mail.smtp.host", "smtp.gmail.com");
	      properties.put("mail.smtp.socketFactory.port", "465");
	      properties.put("mail.smtp.socketFactory.class",
	                     "javax.net.ssl.SSLSocketFactory");
	      properties.put("mail.smtp.auth", "true");
	      properties.put("mail.smtp.port", "465");
	   }
	   
	   //sending verification mail to registered user
	   public String sendverification(String username, String email, String verificationurl) 
	   {
		  MyLog.log("Inside sendverification function");
	      String ret = "success";
	      
	      //settings for sending mail
	      setFrom(RuntimeSettings.smtpFrom);
	      setPassword(RuntimeSettings.smtpPassword);
	      setTo(email);
	      setSubject("Email Varification");
	      setBody("Activation Key \n\n" + verificationurl);
	      //System.out.println("mail");
	      try
	      {
	         Session session = Session.getDefaultInstance(properties,  
	            new javax.mail.Authenticator() {
	            protected PasswordAuthentication 
	            getPasswordAuthentication() {
	            return new 
	            PasswordAuthentication(from, password);
	            }});

	         Message message = new MimeMessage(session);
	         message.setFrom(new InternetAddress(from));
	         message.setRecipients(Message.RecipientType.TO, 
	            InternetAddress.parse(to));
	         message.setSubject(subject);
	         message.setText(body);
	         Transport.send(message);
	         //System.out.println("mail sent");
	      }
	      catch(Exception e)
	      {
	         ret = "error";
	         e.printStackTrace();
	      }
	      return ret;
	   }
	   
	   //sending activation mail to registered user
	   public void sendActivation(String username, String mail) {
		  MyLog.log("Inside sendActivation function");
		//settings for sending mail
	      setFrom(RuntimeSettings.smtpFrom);
	      setPassword(RuntimeSettings.smtpPassword);
	      setTo(mail);
	      setSubject("Activation Successful");
	      setBody("Thank You " + username + ", your account has been activated");
	      //System.out.println("mail");
	      try
	      {
	         Session session = Session.getDefaultInstance(properties,  
	            new javax.mail.Authenticator() {
	            protected PasswordAuthentication 
	            getPasswordAuthentication() {
	            return new 
	            PasswordAuthentication(from, password);
	            }});

	         Message message = new MimeMessage(session);
	         message.setFrom(new InternetAddress(from));
	         message.setRecipients(Message.RecipientType.TO, 
	            InternetAddress.parse(to));
	         message.setSubject(subject);
	         message.setText(body);
	         Transport.send(message);
	         //System.out.println("mail sent");
	      }
	      catch(Exception e)
	      {
	         e.printStackTrace();
	      }
		
	}
}
